#pragma once
#include <iostream>
#include <unordered_map>
#include <string>
#include <fstream>
#include "log.hpp"

const std::string dictname = "./dict.txt";  
const std::string symbol=":";   //分隔符

extern Log lg;

class Init
{
public:
    Init()
    {
        //打开文件，已读方式
        std::fstream in(dictname);
        if (!in.is_open())
        {
            lg(Fatal,"fstream in error, errno:%s",errno);
            exit(1);
        }

        //循环读取每行内容
        std::string line;
        while(std::getline(in,line))
        {
            std::string line1,line2;

            //分割
            split(line,&line1,&line2);
            fict_.insert({line1,line2});
        }
    }

    void split(const std::string &line,std::string *line1,std::string *line2)
    {
        auto iter=line.find(symbol);
        if(iter!=std::string::npos)
        {
            *line1=line.substr(0,iter);
            *line2=line.substr(iter+1);
        } 
    }

    std::string translate(const std::string &word)
    {
        auto pos=fict_.find(word);
        if(pos==fict_.end())
        {
            return "Unknow";
        }
        return pos->second;
    }

private:
    //first是英文 second是中文
    std::unordered_map<std::string, std::string> fict_;
};